package com.lvm.app.file;


import com.alibaba.druid.util.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.UUID;
import java.util.regex.Matcher;

public class FileUtil {

    private static FileUtil INSTANCE;

    private FileUtil(){

    }
    public final static FileUtil getInstance(){
         if(INSTANCE==null){
             synchronized (FileUtil.class){
                 if(INSTANCE==null){
                     INSTANCE = new FileUtil();
                 }
             }
         }
         return INSTANCE;
    }

    public String makeDir(String outDir,String taskId,String chileDir){
        LocalDateTime now  =  LocalDateTime.now();
        String dir =  File.separator+now.getYear()+File.separator+now.getMonth().getValue()+File.separator+taskId+File.separator;
        String outDir2 = outDir+dir+(StringUtils.isEmpty(chileDir)?"":(chileDir+File.separator));
        File dirFile = new File(outDir2);
        if(!dirFile.exists()){
            dirFile.mkdirs();
        }
        return outDir2;
    }

    public File saveFile(File file, String outDir, String chileDir,String taskId,TaskCallBack<String> callBack) throws IOException {
        String dir =  makeDir(outDir,taskId,chileDir);
        String fileName = file.getName();
        callBack.callBack(file.getPath()+"->移动开始");
        String suffix = fileName.substring(fileName.indexOf(".")+1);
        String outFileName = UUID.randomUUID()+"."+suffix;
        File outFile = new File(dir+outFileName);
        FileInputStream in = null;
        FileOutputStream out = null;
        try{
            in = new FileInputStream(file);
            out = new FileOutputStream(outFile);
            byte[] bytes = new byte[2048];
            while (in.read(bytes)>0){
                   out.write(bytes);
            }
            callBack.callBack(file.getPath()+"->移动完成");
        }catch (Exception e){
            throw e;
        }finally {
            if(out!=null){
                out.flush();
                out.close();
            }
            if(in!=null){
                in.close();
            }
        }
        return outFile;
    }
    public String getPath(String outDir,File file){
        String o = outDir.replaceAll("\\\\\\\\", Matcher.quoteReplacement("\\"));
        return file.getPath().replace(o,"");
    }
    public String getSuffix(File file){
        if(!file.isDirectory()){
           return file.getName().substring( file.getName().indexOf(".")+1);
        }
        return "";
    }
    public String getName(File file){
        if(!file.isDirectory()){
            return file.getName().substring(0,file.getName().indexOf("."));
        }
        return "";
    }


    public static void main(String[] args) {
        String outDir ="\\\\";
        String o = outDir.replaceAll("\\\\\\\\", Matcher.quoteReplacement("\\"));
        System.out.println(o);
    }
}
